package com.lmabbe.core.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.core.common.converter.SysStationConverter;
import com.lmabbe.core.common.dto.SysStationDTO;
import com.lmabbe.core.common.entitys.SysStation;
import com.lmabbe.core.common.interfaces.SysStationService;
import com.lmabbe.core.common.query.SysStationQuery;
import com.lmabbe.core.common.vo.SysStationVO;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位表管理
 *
 * @author lmabbe
 */
@Api(tags = "岗位表管理")
@RestController
@RequestMapping("sysStation")
@RequiredArgsConstructor
public class SysStationController {

    private final SysStationConverter sysStationConverter;
    @DubboReference
    private SysStationService sysStationService;

    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<SysStationVO>> list() {
        List<SysStation> list = sysStationService.list();
        return R.success(sysStationConverter.list2Vos(list));
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<SysStationVO>> list(SysStationQuery query) {
        IPage page = sysStationService.selectPage(query);
        page.setRecords(sysStationConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<SysStationVO> getById(@PathVariable Long id) {
        return R.success(sysStationConverter.entity2Vo(sysStationService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody SysStationDTO dto) {
        this.sysStationService.save(sysStationConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{stationId}")
    public R update(@RequestBody SysStationDTO dto, @PathVariable Long stationId) {
        this.sysStationService.updateById(sysStationConverter.dto2Entity(dto, stationId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.sysStationService.removeById(id);
        return R.success();
    }

}